<?php

use Doraemon\service\Ideabook;
use Doraemon\tools\Log;

/**
 * description
 *
 * @category   Module name
 * @package    PSR
 * @subpackage Documentation\API
 * @author     weiyizheng
 * @date       2022/9/22
 * @time       10:37 上午
 */

class IdeabookCollectAction extends ActionPlugin
{
	const IDEABOOK_COLLECT_TABLE = [
		'hhz_idea_book_collect_0',
		'hhz_idea_book_collect_1',
		'hhz_idea_book_collect_2',
		'hhz_idea_book_collect_3'
	];
	public function main()
	{
		// 日志
		Log::info('IdeabookCollect操作接收到的订阅消息', $this->messageBody);
		if (!$this->messageBody['database']) throw new \Exception('未获取到数据库');
		if (!$this->messageBody['table']) throw new \Exception('未获取到数据表');
		if (!$this->messageBody['data']) throw new \Exception('未接收到数据');

		if (in_array($this->messageBody['table'], self::IDEABOOK_COLLECT_TABLE)) {
			$data = $this->messageBody['data'];
			$this->updateIdeaBook($data);
		}
		$this->setSuccess();
	}

	public function updateIdeaBook($data)
	{
		if (empty($data['uid']) || empty($data['idea_book_id'])) return false;
		$coverImg = Ideabook::updateIdeaBookCoverImg($data['uid'], $data['idea_book_id']);
		Log::info("update_idea_book_cover_img", ['uid' => $data['uid'], 'idea_book_id' => $data['idea_book_id'], 'cover_img' => implode(",", $coverImg)]);
		return true;
	}
}